#include "setmaster_dialog.h"
#include "ui_setmaster_dialog.h"
#include "user/sys_config.h"
#include <QMessageBox>

SetMaster_Dialog::SetMaster_Dialog(Serial_Tx_Thread *tx_thread , Serial_Rx_Thread *rx_thread, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SetMaster_Dialog)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setGeometry(150, 100, QWidget::width(), QWidget::height());
    //setGeometry(150, 100, 521, 261);

    //ui->buttonBox->button(QDialogButtonBox::Ok)->setText("确 定");
    ui->buttonBox->button(QDialogButtonBox::Close)->setText("关 闭");

    this->tx_thread = tx_thread;
    this->rx_thread = rx_thread;

    connect(this,SIGNAL(signal_write_sp_tx_queue(char*,unsigned int)),tx_thread,SLOT(slot_write_serial_port_tx_queue(char*, unsigned int)));
    connect(this->rx_thread,SIGNAL(signal_get_sys_config(unsigned char*,unsigned char)),this,SLOT(slot_get_sys_config(unsigned char*,unsigned char)));
    connect(this->rx_thread,SIGNAL(signal_set_sys_config(unsigned char*,unsigned char)),this,SLOT(slot_set_sys_config(unsigned char*,unsigned char)));
}


SetMaster_Dialog::~SetMaster_Dialog()
{
    delete ui;
}

void SetMaster_Dialog::slot_get_sys_config(unsigned char *p_data,unsigned char len)
{
    qDebug() << ">>> slot_get_sys_config >>>";

    QByteArray ba_data((char*)p_data,(int)len);
    QString str_data = ba_data.toHex();

    qDebug() << str_data << len;

    sys_config_t sys_config_get;

    memcpy(&sys_config_get,p_data,sizeof(sys_config_t));


    if(sys_config_get.net_id == 10000)ui->comboBox_net_id->setCurrentIndex(NET_ID_0);
    else if(sys_config_get.net_id == 10001)ui->comboBox_net_id->setCurrentIndex(NET_ID_1);
    else if(sys_config_get.net_id == 10002)ui->comboBox_net_id->setCurrentIndex(NET_ID_2);
    else if(sys_config_get.net_id == 10003)ui->comboBox_net_id->setCurrentIndex(NET_ID_3);

    //dev id
    ui->comboBox_dev_id->setCurrentIndex(0);

    //channel
    ui->comboBox_channel->setCurrentIndex(sys_config_get.channel - 1);

    //dev phy
    ui->comboBox_dev_phy->setCurrentIndex(sys_config_get.dev_phy - 1);

    //dev role
    ui->comboBox_dev_role->setCurrentIndex(0);

    //dev type
    ui->comboBox_dev_type->setCurrentIndex(0);


    ui->label_state->setText(tr("<font color='white'>获取参数成功</font>"));
}


void SetMaster_Dialog::slot_set_sys_config(unsigned char *p_data,unsigned char len)
{
    //qDebug() << "slot_set_sys_config";

    if((p_data[0] == RET_SUCCESS) && (len == 1))
    {
        ui->label_state->setText(tr("<font color='white'>设置参数成功</font>"));

    }

}

void SetMaster_Dialog::on_pushButton_get_params_clicked()
{
    unsigned int data_len = 0;
    TYP_UN_COMMUNICATE_BUFF p_data_send;
    My_Protocol my_protocol;

    data_len = my_protocol.protocol_data_package(CMD_GET_SYS_CONFIG,0,nullptr,&p_data_send);

    emit signal_write_sp_tx_queue((char*)&p_data_send.databuff,data_len);
}


void SetMaster_Dialog::on_pushButton_set_params_clicked()
{
//    QString str_send;
//    str_send = QString(">>> on_pushButton_set_params_clicked >>> \r\n");
//    emit signal_write_sp_tx_queue(str_send.toLatin1().data(),(unsigned int)str_send.length());


    unsigned int data_len = 0;
    TYP_UN_COMMUNICATE_BUFF p_data_send;
    My_Protocol my_protocol;
    sys_config_t sys_config_set;
    bool ok;

    QString str_net_id = ui->comboBox_net_id->currentText();
    QString str_dev_id = ui->comboBox_dev_id->currentText();
    QString str_channel = ui->comboBox_channel->currentText();
    QString str_dev_phy = ui->comboBox_dev_phy->currentText();
    QString str_dev_role = ui->comboBox_dev_role->currentText();
    QString dev_type = ui->comboBox_dev_type->currentText();

    //net_id
    sys_config_set.net_id = (unsigned short)str_net_id.toUShort(&ok,10);
    //dev_id
    sys_config_set.dev_id = (unsigned short)str_dev_id.toUShort(&ok,10);
    //channel
    if(str_channel == "CHN_1")sys_config_set.channel = CHANNEL_ONE;
    else if(str_channel == "CHN_2")sys_config_set.channel = CHANNEL_TWO;
    else if(str_channel == "CHN_3")sys_config_set.channel = CHANNEL_THREE;
    else if(str_channel == "CHN_4")sys_config_set.channel = CHANNEL_FOUR;
    //dev_phy
    if(str_dev_phy == "433MHz")sys_config_set.dev_phy = PHY_433MHZ_MODULE;
    else if(str_dev_phy == "2.4GHz")sys_config_set.dev_phy = PHY_2_4GHZ_MODULE;
    //dev_role
    sys_config_set.dev_role = MASTER_ROLE;
    //dev_type
    sys_config_set.dev_type = DEV_TYPE_ARM_EXTERN;
    //baudrate
    sys_config_set.baudrate = BAUDRATE_115200;
    //params
    sys_config_set.param_flag = 0x5FF5;

    data_len = my_protocol.protocol_data_package(CMD_SET_SYS_CONFIG,sizeof(sys_config_t),(unsigned char*)&sys_config_set,&p_data_send);

    emit signal_write_sp_tx_queue((char*)&p_data_send.databuff,data_len);
}
